package ink.yql.controller;

import ink.yql.model.Returns;
import ink.yql.service.ReturnsService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author YQL
 */
@RestController
@RequestMapping("/returns")
public class ReturnsController {
    @Autowired
    private ReturnsService returnsService;

    @GetMapping("/{pid}/{sendTime}")
    public RestObject getByPid(@PathVariable("pid") Integer pid, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return returnsService.getByPid(pid);
    }
    @PostMapping("/{sendTime}")
    public RestObject addOne(@RequestBody Returns returns, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return returnsService.addOne(returns);
    }
    @PutMapping("/{sendTime}")
    public RestObject update(@RequestBody Returns returns, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return returnsService.update(returns);
    }
    @DeleteMapping("/{rid}/{sendTime}")
    public RestObject update(@PathVariable("rid")Integer rid, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return returnsService.deleteRid(rid);
    }

}
